package com.example.travel.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.travel.R;
import com.example.travel.bean.AddDynamic;

import java.util.List;

import cn.bmob.v3.datatype.BmobFile;

public class MyAdapter extends BaseAdapter {
    private Context context;
    private List<AddDynamic> list;

    public MyAdapter(Context context, List<AddDynamic> list) {
        this.context = context;
        this.list = list;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder;
        if (convertView == null) {
            String name;
            String desc;
            String title;
            double price;
            String creatdeAt;
            BmobFile image;
//            name = list.get(position).getName();
            title = list.get(position).getTitle();
            desc = list.get(position).getDesc();
            creatdeAt =list.get(position).getCreatedAt();

//            price = list.get(position).getPrice();
//            image = list.get(position).getIcon();
//            System.out.println(image.getFileUrl());
            LayoutInflater inflater = LayoutInflater.from(context);
            convertView = inflater.inflate(R.layout.dongtaibuju, null);//实例化一个布局文件
            viewHolder = new ViewHolder();
//            viewHolder.iv_img = (ImageView) convertView.findViewById(R.id.iv);
//            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.tv_title = (TextView) convertView.findViewById(R.id.tv1);
            viewHolder.tv_desc = (TextView) convertView.findViewById(R.id.tv2);
            viewHolder.tv_time = (TextView) convertView.findViewById(R.id.tv3);
            convertView.setTag(viewHolder);
            //不能直接在主线程中进行从网络端获取图片，而需要单独开一个子线程完成从网络端获取图片
//            new Thread(new Runnable() {
//                @Override
//                public void run() {
//                    //根据表中图片的url地址来得到图片（Bitmap类型）
//                    final Bitmap bitmap = getPicture(list.get(position).getIcon().getFileUrl());
//                    try {
//                        Thread.sleep(2000);
//                    } catch (InterruptedException e) {
//                        e.printStackTrace();
//                    }
//                    viewHolder.iv_img.post(new Runnable() {
//                        @Override
//                        public void run() {
//                            System.out.println("*********************************");
//                            viewHolder.iv_img.setImageBitmap(bitmap);//将图片放到视图中去
//                        }
//                    });
//                }
//            }).start();
            viewHolder.tv_title.setText(title);
            viewHolder.tv_time.setText(String.valueOf(creatdeAt));   //将double数据放到textView中
            viewHolder.tv_desc.setText(desc);

        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        return convertView;
    }

    class ViewHolder {
        ImageView iv_img;
        TextView tv_title;
        TextView tv_time;
        TextView tv_desc;
    }
}
